package cc.orange.utils;

import android.content.Context;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import cc.orange.BuildConfig;
import cc.orange.R;
import cc.orange.mainView.WebViewActivity;


public class LoginsNotifyDialog extends BaseDialog implements OnClickListener {

    private TextView ln_text,ln_sure,ln_cancle;

    public LoginsNotifyDialog(Context context) {
        super(context);
        View vv = LayoutInflater.from(context).inflate(
                R.layout.item_logins_notify_dialog, null);

        addView(vv,2);
        ln_text = (TextView) vv.findViewById(R.id.ln_text);
        String s = "<font color=\"#666666\">请你务必审慎阅读、充分理解" +
                "<font ><a href= "+ BuildConfig.WEBS_URL1+">《服务协议》</a ></font>" +
                "和<font ><a href="+BuildConfig.WEBS_URL2+">《隐私政策》</a ></font>" +
                "各条款。" +
                "<br/>应用中相关权限，协议与申请说明，请详细阅读"+
                "<font ><a href= "+BuildConfig.WEBS_URL1+">《服务协议》</a ></font>"+
                "和<font ><a href="+BuildConfig.WEBS_URL2+">《隐私政策》</a ></font>"+
                "了解详细信息或通过您手机中的设置进行管理本软件权限。如你同意请点击”同意”开始接受我们的服务。<br/></font>";
        ln_text.setText(getClickableHtml(context,s));
        //这一句很重要，否则ClickableSpan内的onClick方法将无法触发！！ color="#F48337"
        ln_text.setMovementMethod(LinkMovementMethod.getInstance());
        ln_cancle = (TextView) vv.findViewById(R.id.ln_cancle);
        ln_sure = (TextView) vv.findViewById(R.id.ln_sure);

        ln_sure.setOnClickListener(this);
        ln_cancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        l.onClick(view);
        dismiss();
    }


    /**
     * 格式化超链接文本内容并设置点击处理
     * */
    public static CharSequence getClickableHtml(Context context,String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for(final URLSpan span : urls) {
            setLinkClickable(context,clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    /**
     * 设置点击超链接对应的处理内容
     * */
    public static void setLinkClickable(final Context context, final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClicksSpans clickableSpan = new ClicksSpans() {
            @Override
            public void onClick(View view) {
                if(urlSpan.getURL().contains("yonghu")){//隐私政策
                    WebViewActivity.openH5Activity(context,"隐私政策",BuildConfig.WEBS_URL2);
                }else if(urlSpan.getURL().contains("xieyi")){
                    WebViewActivity.openH5Activity(context,"服务协议",BuildConfig.WEBS_URL1);
                }
            }
        };
        //实际使用过程中，可根据需要清空之前的span，防止出现重复调用的情况
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }
}
